<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Test;
use App\Admin\Actions\RefundOrder;
use App\Admin\Actions\SendOrder;
use App\Admin\Repositories\TongOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Filesystem\Cache;
use Illuminate\Http\Request;

class TongOrderController extends AdminController
{
    protected $title = '订单列表';


    protected $status = 0;
    public function __construct(Request $request)
    {
        $this->status = $request->status;
        return $this;
    }

    /**
     * @return Grid
     */
    protected function grid()
    {
        $array = [-1=>'全部',0=>'待支付',1=>'支付成功',2=>'支付失败',3=>'待发货',4=>'待收货',5=>'已收货',6=>'退货',7=>'退货收货'];

        return Grid::make(new TongOrder(['userInfo']), function (Grid $grid) use($array) {
//            $grid->wrap(function(Renderable $view)use ($array){
//                $tab = Tab::make();
//                $tab->add($array[0],$view,true);
//                $tab->add($array[1],$view,true);
//                $tab->add($array[2],$view,true);
//                $tab->add($array[3],$view,true);
//                $tab->add($array[4],$view,true);
//                $tab->add($array[5],$view,true);
//                $tab->add($array[6],$view,true);
//                $tab->add($array[7],$view,true);
//                return $tab;
//            });

            $grid->header(function () {
                $tab = Tab::make();
                $tab->addLink('全部', '?status=-1',$this->status==-1 ? true : false);
                $tab->addLink('待支付', '?status=0',$this->status==0 ? true : false);
                $tab->addLink('待发货', '?status=1',$this->status==1 ? true : false);
                $tab->addLink('待收货', '?status=4',$this->status==4 ? true : false);
                $tab->addLink('已收货', '?status=5',$this->status==5 ? true : false);
                $tab->addLink('退货', '?status=6',$this->status==6 ? true : false);
                $tab->addLink('退货收货', '?status=7',$this->status==7 ? true : false);
                return $tab;
            });

            //监听表格再获取数据之前
            $grid->listen(Grid\Events\Fetching::class, function ($grid) {
                //dd($this->status);
                if(is_null($this->status)){
                    $this->status = 0;
                }
            });


            if($this->status!=-1){
                $grid->model()->where('status',is_null($this->status) ? $this->status = 0 : $this->status);
            }


            $grid->model()->where('is_test',0); //测试订单不展示

            $grid->column('id','订单ID')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->column('order_no','HB订单编号');
            $grid->column('order_type','订单类型')->display(function($value){
                if($value==1){
                    return '普通订单';
                }else{
                    return '套餐订单';
                }
            });
            $grid->column('is_test','测试')->display(function($value){
                if($value==1){
                    return '测试订单';
                }else{
                    return '正常订单';
                }
            });
            $grid->column('u_id','用户名')->display(function($value){
                return $this->userInfo['nickname'];
            });
            $grid->column('goods_id','商品名')->display(function($value){
                if($this->order_type==1){
                    return $this->order_goods_info['goods_name'];
                }else{
                    return $this->order_group_goods_info['goods_group_name'];
                }
            });
            $grid->column('goods_num')->display(function($value){
                return $value.'件';
            });
            $grid->column('order_price');
            $grid->column('all_order_price');
            $grid->column('status','订单状态')->display(function ($value){
                if($value==0){
                    return '待支付';
                }elseif ($value ==1){
                    return '支付成功，待发货';
                }elseif($value==2){
                    return '支付失败';
                }elseif ($value==4){
                    return '发货成功，待收货';
                }
            });
            $grid->column('pay_status','支付方式')->display(function($value){
                if($this->status==0){
                    return '暂未支付';
                }elseif($value==0 && $this->status!=0){
                    return '钱包支付';
                }elseif($value==1 && $this->status!=0){
                    return '微信支付';
                }elseif ($value==2 && $this->status!=0){
                    return '复购券支付';
                }
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id','订单id')->placeholder('请输入订单id')->width(3);
                $filter->like('order_no','HB订单编号')->placeholder('请输入HB订单编号')->width(3);
                $filter->like('orderAddress.name','收货人')->placeholder('请输入收货人')->width(3);
                $filter->like('orderAddress.mobile','收货手机号')->placeholder('请输入收货手机号')->width(3);
                $filter->like('orderGoods.goods_name','商品名称')->placeholder('请输入收商品名称')->width(3);
                $filter->whereBetween('created_at',function ($q) {
                    $start = $this->input['start'] ?? null;
                    $end = $this->input['end'] ?? null;
                    if ($start !== null) {
                        $q->where('created_at', '>=', $start);
                    }

                    if ($end !== null) {
                        $q->where('created_at', '<=', $end);
                    }

                },'创建时间')->datetime()->width(6);
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
//                $actions->disableQuickEdit();
//                $actions->disableView();
                $actions->append(new RefundOrder(\App\Models\TongOrder::class));
            });
            $grid->toolsWithOutline(false);

            // 禁用添加按钮
            $grid->disableCreateButton();
            //禁用复选框
            $grid->disableRowSelector();
            //启用导出功能
            $titles = ['order_no' => '商户单号','mchid'=>'商户号','send_type'=>'发货方式','send_type_model'=>'发货模式','express_company'=>'快递公司','express_no'=>'快递单号（多个快递单使用;分隔）','is_over'=>'是否完成发货','is_reload'=>'是否重新发货','shop_info'=>'商品信息'];
            $grid->export($titles)->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    //查询出订单微信支付的订单并且是待发货的
                    if($row['pay_status']==1 && $row['status']==1){
                        $row['send_type'] = '虚拟发货';
                        $row['send_type_model'] = '统一发货';
                        //$row['shop_info'] = $row['']
                    }else{
                        continue;
                    }

                }

                return $rows;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
//        return Show::make($id, new TongOrder(), function (Show $show) {
//            $show->field('id');
//            $show->field('order_no');
//            $show->field('user_id');
//            $show->field('goods_id');
//            $show->field('goods_num');
//            $show->field('order_price');
//            $show->field('all_order_price');
//            $show->field('order_goods_info');
//            $show->field('order_address_info');
//            $show->field('status');
//            $show->field('pay_status');
//            $show->field('express_num');
//            $show->field('pay_time');
//            $show->field('send_time');
//            $show->field('created_at');
//            $show->field('updated_at');
//        });
        return Show::make($id,new TongOrder(),function (Show $show){
            if($show->model()->status==1){
//                $show->tools('<a class="btn btn-sm btn-default">发货</a>');
                $show->tools(SendOrder::make()->payload(['id'=>$show->model()->id]));
            }
            $show->html(view('admin.order.order_info', ['order' => $show->model()]));
            $show->disableDeleteButton();
            $show->disableEditButton();
        });


    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TongOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_no');
            $form->text('user_id');
            $form->text('goods_id');
            $form->text('goods_num');
            $form->text('order_price');
            $form->text('all_order_price');
            $form->text('order_goods_info');
            $form->text('order_address_info');
            $form->text('status');
            $form->text('pay_status');
            $form->text('express_num');
            $form->text('pay_time');
            $form->text('send_time');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
